package com.imooc.order.controller;

import com.imooc.order.client.ProductClient;
import com.imooc.order.dataobject.ProductInfo;
import com.imooc.order.dto.CartDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;

/**
 * @Name com.imooc.order.controller.ClientController
 * @Description
 * @Author Elwyn
 * @Version 2020/3/17
 * @Copyright 上海云辰信息科技有限公司
 **/
@RestController
@Slf4j
public class ClientController {
	//@Autowired
	//private LoadBalancerClient loadBalancerClient;

//	@Autowired
//	private RestTemplate restTemplate;

	@Autowired
	private ProductClient productClient;

	@RequestMapping("/getProductMsg")
	public String getProductMsg() {
		//RestTemplate restTemplate = new RestTemplate();
		//String response = restTemplate.getForObject("http://localhost:8080/msg", String.class);
		//2
//		ServiceInstance serviceInstance = loadBalancerClient.choose("PRODUCT");
//		String url = String.format("http://%s:%s", serviceInstance.getHost(), serviceInstance.getPort() + "/msg");
//		RestTemplate restTemplate = new RestTemplate();
//		String response = restTemplate.getForObject(url, String.class);
		//3
		//String response = restTemplate.getForObject("http://PRODUCT/msg", String.class);
		String response = productClient.productMsg();
		log.info("response={}", response);
		return response;
	}

	@GetMapping("/getProductList")
	public String getProductList() {
		List<ProductInfo> productInfos = productClient.listForOrder(Arrays.asList("1", "2"));
		log.info("productInfos={}", productInfos);
		return "ok";
	}

	@GetMapping("/productDecreaseStock")
	public String productDecreaseStock() {
		productClient.decreaseStock(Arrays.asList(new CartDTO("1",3)));
		return "ok";
	}
}
